package com.web.base.core.util;

import com.blue.jdbc.Page;
import com.web.base.core.message.AreaRequest;
import com.web.base.core.message.AreaResponse;
import com.web.base.core.model.Area;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 区域转换器
 * 
 * @author 代码由 blue-code 框架生成
 * @since 1.0 2018年06月19日
 */
public class AreaConvertor
{
	private AreaConvertor()
	{
	}
	
	public static Area toArea(AreaRequest request)
	{
		if (request == null)
			return null;
		
		Area area = new Area();
		area.setId(request.getId());
		area.setParentId(request.getParentId());
		area.setCarCode(request.getCarCode());
		area.setName(request.getName());
		area.setState(request.getState());
		return area;
	}
	
	public static List<Area> toArea(List<AreaRequest> requestList)
	{
		List<Area> list = new ArrayList<>();
		if (requestList == null || requestList.isEmpty())
			return list;
		
		for (AreaRequest request : requestList)
		{
			list.add(toArea(request));
		}
		return list;
	}
	
	public static AreaResponse toResponse(Area area)
	{
		if (area == null)
			return null;
		
		AreaResponse response = new AreaResponse();
		response.setId(area.getId());
		response.setParentId(area.getParentId());
		response.setCarCode(area.getCarCode());
		response.setName(area.getName());
		response.setState(area.getState());
		return response;
	}
	
	public static List<AreaResponse> toResponse(List<Area> list)
	{
		List<AreaResponse> responseList = new ArrayList<>();
		if (list == null || list.isEmpty())
			return responseList;
		
		for (Area area : list)
		{
			responseList.add(toResponse(area));
		}
		return responseList;
	}

	public static Map<Integer, AreaResponse> toAreaResponse(Map<Integer, Area> map)
	{
		Map<Integer, AreaResponse> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;

		for (Map.Entry<Integer, Area> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue()));
		}
		return responseMap;
	}
	
	public static Map<String, List<AreaResponse>> toResponse(Map<String, List<Area>> map)
	{
		Map<String, List<AreaResponse>> responseMap = new HashMap<>();
		if (map == null || map.isEmpty())
			return responseMap;
		
		for (Map.Entry<String, List<Area>> entry : map.entrySet())
		{
			responseMap.put(entry.getKey(), toResponse(entry.getValue()));
		}
		return responseMap;
	}
	
	public static Page<AreaResponse> toResponse(Page<Area> page)
	{
		if (page == null)
			return null;
		
		Page<AreaResponse> responsePage = new Page<>();
		responsePage.setCurPage(page.getCurPage());
		responsePage.setRows(page.getRows());
		responsePage.setSize(page.getSize());
		responsePage.setStartRowNo(page.getStartRowNo());
		responsePage.setTotalPage(page.getTotalPage());
		responsePage.setObjectList(toResponse(page.getObjectList()));
		return responsePage;
	}
	
}
